<?php

namespace app\modules\Customer\models;

use Yii;
use app\modules\Product\models\Product;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "customer_contact".
 *
 * @property int $id
 * @property int $product_id 团队ID
 * @property int $customer_id 团队ID
 * @property string $p_name 联系人姓名
 * @property string $p_phone 联系人电话
 * @property string $p_mail 联系人邮箱
 * @property string $p_department 联系人部门
 * @property int $status 1正常  2 禁用 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class CustomerContact extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'customer_contact';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'customer_id', 'status', 'user_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['p_name', 'p_phone', 'p_mail', 'p_department'], 'string', 'max' => 128],
        ];
    }

     //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        return parent::beforeSave($insert);

    }//}}}

    //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    //  获取团队信息
    public function getProduct(){
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
    }
    
    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app.product', 'Product ID'),
            'customer_id' => Yii::t('app', 'Customer ID'),
            'p_name' => Yii::t('app.product', 'P Name'),
            'p_phone' => Yii::t('app.product', 'P Phone'),
            'p_mail' => Yii::t('app.product', 'P Mail'),
            'p_department' => Yii::t('app.product', 'P Department'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
